/*
 * Copyright (c) 2011, grossmann
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * * Neither the name of the jo-widgets.org nor the
 *   names of its contributors may be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL jo-widgets.org BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */

package joodin.impl.widgets.internal;

import joodin.impl.image.VaadinImageRegistry;
import joodin.impl.widgets.VaadinComposite;
import joodin.impl.widgets.VaadinContainer;

import org.jowidgets.common.color.IColorConstant;
import org.jowidgets.common.image.IImageConstant;
import org.jowidgets.common.types.Cursor;
import org.jowidgets.common.types.Dimension;
import org.jowidgets.common.widgets.IControlCommon;
import org.jowidgets.common.widgets.controler.IPopupDetectionListener;
import org.jowidgets.common.widgets.descriptor.IWidgetDescriptor;
import org.jowidgets.common.widgets.factory.ICustomWidgetFactory;
import org.jowidgets.common.widgets.factory.IGenericWidgetFactory;
import org.jowidgets.common.widgets.layout.ILayoutDescriptor;
import org.jowidgets.spi.impl.controler.TabItemObservableSpi;
import org.jowidgets.spi.widgets.IPopupMenuSpi;
import org.jowidgets.spi.widgets.ITabItemSpi;
import org.jowidgets.spi.widgets.setup.ITabItemSetupSpi;

import com.example.vmiglayout.MigLayout;
import com.vaadin.terminal.Resource;
import com.vaadin.ui.Component;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.TabSheet.SelectedTabChangeEvent;
import com.vaadin.ui.TabSheet.SelectedTabChangeListener;
import com.vaadin.ui.TabSheet.Tab;
import com.vaadin.ui.themes.Runo;

public class TabItemImpl extends TabItemObservableSpi implements ITabItemSpi {

	private final VaadinContainer vaadinContainer;
	private Tab tabItem;
	private TabSheet parentFolder;
	private SelectedTabChangeListener tabSelectionListener; 
	private boolean detached = false;
	private static Component detachedControl;
	private String text;
	private String toolTipText;
	private Resource icon;


	public TabItemImpl(final IGenericWidgetFactory factory, final TabSheet parentFolder, final ITabItemSetupSpi setup, boolean closeable) {
		this(factory, parentFolder, setup, null, closeable);
	}

	@SuppressWarnings("serial")
	public TabItemImpl(
		final IGenericWidgetFactory factory,
		final TabSheet parentFolder,
		final ITabItemSetupSpi setup,
		final Integer index, 
		final boolean closeable) {
		
		this.parentFolder = parentFolder;
		Panel content = new Panel();
		content.addStyleName(Runo.PANEL_LIGHT);
		content.setSizeFull();
		
		if (index != null) {
			tabItem = parentFolder.addTab(content, index);
		}
		else {
			tabItem = parentFolder.addTab(content);
		}
		
		tabItem.setClosable(closeable);
		
		content.setContent(new MigLayout("", "[]", "[]"));

		vaadinContainer = new VaadinComposite(factory, content);
		
		tabSelectionListener = new SelectedTabChangeListener() {
			@Override
			public void selectedTabChange(SelectedTabChangeEvent event) {
				fireSelected();
			}
		};
	
		parentFolder.addListener(tabSelectionListener);
	}
	
	public void detach() {
		detached = true;
		detachedControl = tabItem.getComponent();
		parentFolder.removeTab(tabItem);
		tabItem = null;
	}

	public boolean isDetached() {
		return detached;
	}

	public void attach(final TabSheet parentFolder, final boolean closeable, final Integer index) {
		tabItem = createItem(parentFolder, closeable, index);
		setText(text);
		setToolTipText(toolTipText);
		getUiReference().setIcon(icon);
		
		detached = false;
	}
	
	private static Tab createItem(final TabSheet parentFolder, final boolean closeable, final Integer index) {
		Tab result;
		if (index != null) {
			result = parentFolder.addTab(detachedControl, index);
			result.setClosable(closeable);
		}
		else {
			result = parentFolder.addTab(detachedControl);
			result.setClosable(closeable);
		}
		return result;
	}

	@Override
	public Tab getUiReference() {
		return tabItem;
	}

	@Override
	public void setText(final String text) {
		if (text != null) {
			getUiReference().setCaption(text);
			this.text = text;
		}
		else {
			getUiReference().setCaption(String.valueOf(""));
			this.text = "";
		}
	}

	@Override
	public void setToolTipText(final String text) {
		if(text != null){
			getUiReference().setDescription(text);
			this.toolTipText = text;
		}
		else{
			getUiReference().setDescription("");
			this.toolTipText = "";
		}
		
	}

	@Override
	public void setIcon(final IImageConstant icon) {
		this.icon = VaadinImageRegistry.getInstance().getImage(icon);
		getUiReference().setIcon(this.icon);
	}

	@Override
	public void setVisible(final boolean visible) {
		//TODO MG
		vaadinContainer.setVisible(visible);
	}

	@Override
	public boolean isVisible() {
		// TODO MG
		return false;
	}

	@Override
	public void setEnabled(final boolean enabled) {
		vaadinContainer.setEnabled(enabled);
	}

	@Override
	public boolean isEnabled() {
		return vaadinContainer.isEnabled();
	}

	@Override
	public <WIDGET_TYPE extends IControlCommon> WIDGET_TYPE add(
		final IWidgetDescriptor<? extends WIDGET_TYPE> descriptor,
		final Object layoutConstraints) {
		return vaadinContainer.add(descriptor, layoutConstraints);
	}

	@Override
	public <WIDGET_TYPE extends IControlCommon> WIDGET_TYPE add(
		final ICustomWidgetFactory<WIDGET_TYPE> factory,
		final Object layoutConstraints) {
		return vaadinContainer.add(factory, layoutConstraints);
	}

	@Override
	public boolean remove(final IControlCommon control) {
		return vaadinContainer.remove(control);
	}

	@Override
	public IPopupMenuSpi createPopupMenu() {
		return vaadinContainer.createPopupMenu();
	}

	@Override
	public void redraw() {
		vaadinContainer.redraw();
	}

	@Override
	public void setForegroundColor(final IColorConstant colorValue) {
		vaadinContainer.setForegroundColor(colorValue);
	}

	@Override
	public void setBackgroundColor(final IColorConstant colorValue) {
		vaadinContainer.setBackgroundColor(colorValue);
	}

	@Override
	public IColorConstant getForegroundColor() {
		return vaadinContainer.getBackgroundColor();
	}

	@Override
	public IColorConstant getBackgroundColor() {
		return vaadinContainer.getBackgroundColor();
	}

	@Override
	public void setCursor(final Cursor cursor) {
		vaadinContainer.setCursor(cursor);
	}

	@Override
	public Dimension getSize() {
		return vaadinContainer.getSize();
	}

	@Override
	public void addPopupDetectionListener(final IPopupDetectionListener listener) {
		vaadinContainer.addPopupDetectionListener(listener);
	}

	@Override
	public void removePopupDetectionListener(final IPopupDetectionListener listener) {
		vaadinContainer.removePopupDetectionListener(listener);
	}

	@Override
	public void setLayout(final ILayoutDescriptor layoutDescriptor) {
		vaadinContainer.setLayout(layoutDescriptor);
	}

	@Override
	public void layoutBegin() {
		vaadinContainer.layoutBegin();
	}

	@Override
	public void layoutEnd() {
		vaadinContainer.layoutEnd();
	}

	@Override
	public void removeAll() {
		vaadinContainer.removeAll();
	}

	@Override
	public boolean isReparentable() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public void addTabPopupDetectionListener(IPopupDetectionListener listener) {
				
	}

	@Override
	public void removeTabPopupDetectionListener(IPopupDetectionListener listener) {
			
	}

	@Override
	public IPopupMenuSpi createTabPopupMenu() {
		return new PopupMenuImpl(parentFolder);
	}

}
